#include "sled_scene_manager/sled_scene_manager_class/sled_scene_manager_class.h"
#include "sled/sled.h"

#include "debug.h"
#define LOG_TAG "[SLED_SCENE_MANAGER]"
#define SLED_PTR_NULL (void*)0
#define SLED_PTR_SLED_BOARD (void*)1
#define SLED_PTR_ETC       (void*)2

#define log_action(action) \
log_info("node:%p,sled:%p,time:%d,mode:%d,value:%d\n",action,action->sled,action->keep_time_ms,action->sled_mode,action->switch_param.switch_io_state)

static void sled_scene_register(sled_scene_t *scene_array)
{
    sled_scene_action_t *node = NULL;
    logln_pos();
    while(scene_array->scene_name != NULL)
    {
        node = scene_array->actions_array;
        scene_array->cur_action_index = 0;
        while(node->sled != NULL)
        {
            if(node->sled == SLED_PTR_SLED_BOARD)
            {
                node->sled = sled_board;
            }
            else if(node->sled == SLED_PTR_ETC)
            {

            }
            node++;
        }
        scene_array++;
    }
    logln_pos();
}

#define ACTION_END \
{.sled = NULL}

#define SCENE_END \
{.scene_name = NULL}

#define ACTION_ADD_SWITCH(esled,time,switch_state) \
{.sled = esled,.keep_time_ms = time,.sled_mode = SLED_MODE_SWITCH,.switch_param.switch_io_state = switch_state}

#define ACTION_ADD_BREATHE(esled,time,speed) \
{.sled = esled,.keep_time_ms = time,.sled_mode = SLED_MODE_BREATHE,.breathe_param.breath_speed = speed}

#define ACTINO_ADD_PWM(esled,time,duty) \
{.sled = esled,.keep_time_ms = time,.sled_mode = SLED_MODE_PWM,.pwm_param.pwm_duty = duty}

sled_scene_action_t scene_RBFlash3s[] = 
{
    {
        .sled = SLED_PTR_SLED_BOARD,
        .keep_time_ms = 1500,
        .sled_mode = SLED_MODE_SWITCH,
        .switch_param.switch_io_state = SLED_SCENE_SWITCH_IO_STATE_ON_0,
    },
    {
        .sled = SLED_PTR_SLED_BOARD,
        .keep_time_ms = 1500,
        .sled_mode = SLED_MODE_SWITCH,
        .switch_param.switch_io_state = SLED_SCENE_SWITCH_IO_STATE_ON_1,
    },
    ACTION_END
};

sled_scene_action_t scene_RBFlash1_5s[] = 
{
    ACTION_ADD_SWITCH(SLED_PTR_SLED_BOARD,750,SLED_SCENE_SWITCH_IO_STATE_ON_0),
    ACTION_ADD_SWITCH(SLED_PTR_SLED_BOARD,750,SLED_SCENE_SWITCH_IO_STATE_ON_1),
    ACTION_END
};

sled_scene_action_t scene_Breathe3[] = 
{
    {
        .sled = SLED_PTR_SLED_BOARD,
        .keep_time_ms = 1500,
        .sled_mode = SLED_MODE_BREATHE,
        .breathe_param.breath_speed = 2,
    },
    ACTION_END
};

sled_scene_action_t scene_off[] = 
{
    {
        .sled = SLED_PTR_SLED_BOARD,
        .keep_time_ms = 1500,
        .sled_mode = SLED_MODE_SWITCH,
        .switch_param.switch_io_state = SLED_SCENE_SWITCH_IO_STATE_OFF,
    },
    ACTION_END
};

#define SCENE_ADD(bool_loop,name,array) \
{.is_loop = bool_loop,.scene_name = name,.actions_array = array}

#define SCENE_ADD_BY_NAME(bool_loop,name) \
{.is_loop = bool_loop,.scene_name = name,.actions_array = @name}

sled_scene_t sled_scene_board[] =
{
    {
        .is_loop = true,
        .scene_name = "RedAndBlueFlash3s",
        .actions_array = scene_RBFlash3s
    },
    {
        .is_loop = false,
        .scene_name = "Breathe3",
        .actions_array = scene_Breathe3
    },
    {
        .is_loop = false,
        .scene_name = "Off",
        .actions_array = scene_off
    },
    SCENE_ADD(true,"RedAndBlusFlash1_5s",scene_RBFlash1_5s),
    SCENE_END
};

SLED_SCENE_MANAGER_CONSTRUCTOR(led_scene, sled_scene_board, sled_scene_register);